<template>
    <MainSlot>
        <template v-slot:searchForm>
            <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag"
                :key="updateKey" @search="search" @isUnfold="(v) => {
                unfoldFlag = v;
            }
                ">
            </SearchForm>
        </template>
        <template v-slot:btn>
            <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
        </template>
        <template v-slot:table>
            <el-table v-loading="loading" :data="tableData" border ref="tabRef" show-summary height="100%">
                <el-table-column type="index" label="序号" width="55" align="center" />
                <el-table-column align="center" prop="dbilldate" label="日期"></el-table-column>
                <el-table-column align="center" prop="cdptname" label="塘号"></el-table-column>
                <!-- <el-table-column align="center" prop="nfactor" label="面积"></el-table-column> -->
                <el-table-column align="center" label="用药记录">
                    <el-table-column align="center" prop="vnote" label="诊断原因"></el-table-column>
                    <el-table-column align="center" prop="cmaterialvname" label="用药名称"></el-table-column>
                    <!-- <el-table-column align="center" prop="materialspec" label="规格"></el-table-column> -->
                    <el-table-column align="center" prop="castunitname" label="单位"></el-table-column>
                    <el-table-column align="center" prop="nassistnum" label="数量"></el-table-column>
                </el-table-column>
            </el-table>
        </template>
        <template v-slot:pagination>
            <!-- <Pagination @current-change="handleCurrentChange" :page="page" :total="total">
            </Pagination> -->
            <el-pagination @size-change="(e) => { handleSizeChange(e), customSize = e }"
                @current-change="handleCurrentChange" :current-page="page"
                :page-sizes="[10, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, customSize]" :page-size="pageSize"
                layout="total,slot, sizes, prev, pager, next, jumper" background :total="total">
                <template #default>
                    <el-input type="number" style="width: 150px;" v-model="customSize" placeholder="请输入内容" @change="() => {
                customSize = customSize < 1 ? customSize = 1 : Math.floor(customSize)
                handleSizeChange(customSize);
            }"><template slot="append">条/页</template></el-input>
                </template>
            </el-pagination>
        </template>
        <template v-slot:dialogModule>
            <waterAddEdit v-model="addEditShow" :stordocList="stordocList" @close="closeDia" @reflash="getTableList" />
            <waterImport v-model="importShow" @close="closeDia" @reflash="getTableList" />
        </template>
    </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from "@/components-production-management/Pagination";
import BtnSlot from "@/components-production-management/BtnSlot";
import waterAddEdit from "./components/waterAddEdit";
import waterImport from "./components/waterImport";
import reportApi from "@/api/module/medication-report.js";
import TableData from "@/components-production-management/TableData";
import SearchForm from "@/components-production-management/SearchForm";
import ncBasicinfoApi from "@/api/nc-basic-info/index";
import { mymixin } from "@/utils/mixin";
export default {
    mixins: [mymixin],
    components: {
        MainSlot,
        Pagination,
        BtnSlot,
        waterAddEdit,
        TableData,
        SearchForm,
        waterImport,
    },
    data() {
        return {
            formItemList: [
                {
                    label: "塘号",
                    type: "input",
                    param: "cdptname",
                    isShow: true
                },
                {
                    label: "日期查询",
                    type: "daterange",
                    defaultSelect: [],
                    param: "dateTime",
                    isShow: true,
                },
            ],
            unfoldFlag: false,
            tableThead: [
                {
                    isShow: true,
                    prop: "dbilldate",
                    label: "日期",
                },
                {
                    isShow: true,
                    prop: "cdptname",
                    label: "塘号",
                },
                {
                    isShow: true,
                    prop: "cmaterialoname",
                    label: "品种",
                },
                {
                    isShow: true,
                    prop: "nnumsum",
                    label: "重量",
                },
                {
                    isShow: true,
                    prop: "vbdef1sum",
                    label: "条数",
                },
            ],
            btnList: ["export"],
            addEditShow: false,
            tableData: [],
            loading: false,
            page: 1,
            total: 0,
            pageSize: 100,
            customSize: 100,
            stordocList: [],
            updateKey: 0,
            filterData: {},
            importShow: false,

        };
    },
    created() {
        this.queryStordoc();
        this.getTableList();
    },
    mounted() { },
    methods: {
        search(data) {
            this.filterData = JSON.parse(JSON.stringify(data));
            this.page = 1;
            this.getTableList();
        },
        getTableList() {
            const { page, pageSize, filterData } = this;
            const paylod = {
                page,
                size: pageSize,
            };
            const entity = JSON.parse(JSON.stringify(filterData));
            if (entity.dateTime && entity.dateTime.length > 0) {
                entity.startTime = filterData.dateTime[0] + " 00:00:00";
                entity.endTime = filterData.dateTime[1] + " 23:59:59";
                delete entity.dateTime;
            }
            if (filterData.ncWarehouseidLists) {
                entity.ncWarehouseidLists = filterData.ncWarehouseidLists.toString();
            }
            paylod.entity = entity;
            this.loading = true;
            reportApi
                .queryPageGeneraloutReportByCondition(paylod)
                .then((res) => {
                    if (res.success) {
                        const { data } = res;
                        this.tableData = data.content;
                        this.total = data.totalElements;
                        this.tableData = this.tableData.map(item => {
                            for (let key in item) {
                                if (item[key] === null || item[key] === undefined) {
                                    item[key] = '/'
                                }
                            }
                            return {
                                ...item
                            }
                        })
                        if (res.data.content.length === 0 && this.page !== 1) {
                            this.page--;
                            this.getTableList();
                        }
                    }
                })
                .finally(() => {
                    this.loading = false;
                });
        },
        queryStordoc() {
            ncBasicinfoApi.queryFishPondNc().then((res) => {
                this.stordocList = res.data;
                this.formItemList[0].selectOptions = this.stordocList.map((data) => {
                    return {
                        text: data,
                        value: data,
                    };
                });
                this.updateKey++;
            });
        },
        getBtnEvent(v) {
            this["btn" + this.capitalizeFirstLetter(v)]();
        },
        btnAdd() {
            this.addEditShow = true;
        },
        btnExport() {
            const { filterData } = this;
            const entity = JSON.parse(JSON.stringify(filterData));
            if (entity.dateTime && entity.dateTime.length > 0) {
                entity.startTime = filterData.dateTime[0] + " 00:00:00";
                entity.endTime = filterData.dateTime[1] + " 23:59:59";
                delete entity.dateTime;
            }
            if (filterData.ncWarehouseidLists) {
                entity.ncWarehouseidLists = filterData.ncWarehouseidLists.toString();
            }
            reportApi.generaloutReportReport({
                page: this.page,
                size: this.pageSize,
                entity: { ...entity }
            }).then((res) => {
                this.getFileNameAndDownload(res);
            });
        },
        btnImport() {
            this.importShow = true;
        },
        capitalizeFirstLetter(str) {
            if (!str) return "";

            // 将字符串的第一个字符转换为大写，剩余的字符保持不变
            return str.charAt(0).toUpperCase() + str.slice(1);
        },
        handleCurrentChange(page) {
            this.page = page;
            this.getTableList();
        },
        handleSizeChange(size) {
            this.pageSize = size;
            this.getTableList();
        },
        closeDia() {
            this.addEditShow = false;
            this.importShow = false;
            this.getTableList();
        },
    },
};
</script>
<style lang="scss" scoped></style>